package com.controller;
import com.entity.UserInfo;
import com.entity.vo.SubjectVo;
import com.github.pagehelper.PageInfo;
import com.service.ItemService;
import com.service.SubjectService;
import com.service.UserService;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
public class UserController {
	@Resource
	private UserService userService;
	@Resource
	private SubjectService subjectService;
	@Resource
	private ItemService itemService;
	//后台登录方法
	@RequestMapping("/adminuserlogin")
	public String adminLogin(UserInfo user,HttpSession session) {
		UserInfo uinfo = userService.selectUserLogin(user);
		if (uinfo == null) {
			session.setAttribute("uerr", "1");
			session.setAttribute("userinfo", user);
			return "redirect:/adminlogin";
		} else {
			session.setAttribute("user", uinfo);
			return "redirect:/adminmain";
		}
	}
	@RequestMapping("/index") //跳转前台login页面
	public String index() {
		return "login";
	}
	@RequestMapping("/admindeleteUser")
	public String deleteUser(Integer userid,HttpSession session) {
		Integer temp =  this.itemService.selectItemInfoByUserId(userid);
		if(temp == 0) {
			Integer t = this.userService.deleteUser(userid);
			if(t != 0) {
				session.setAttribute("delok", 1);
				//return "redirect:/adminselectUsers";
			}
		}else {
			session.setAttribute("delerr", 1);
			//return "redirect:/adminselectUsers";
		}
		return "redirect:/adminselectUsers";
	}
	@RequestMapping("/updateuser")
	public String updateUser(UserInfo user) {
		Integer temp =  this.userService.updateUser(user);
		return "admin/upok";
	}
	@RequestMapping("/selectOneuser")
	public String selectOneuser(Integer id,Model m) {
			UserInfo uinfo  =  this.userService.selectUserById(id);
			m.addAttribute("uinfo", uinfo);
			return "admin/user";
		}
	@RequestMapping("/list")
	public String list(@RequestParam(defaultValue = "1") Integer index, @RequestParam(defaultValue = "5") Integer size,
			Model m) {
		if (index <= 1) {
			index = 1;
		}
		Integer count = this.subjectService.selectSubjectCount();// ������
		Integer max = count % size == 0 ? count / size : count / size + 1;// ��ҳ��
		if (index >= max) {
			index = max;
		}
		List<SubjectVo> sublist = subjectService.selectSubjectDetailInfo(index, size);
		m.addAttribute("sublist", sublist);
		m.addAttribute("index", index);
		m.addAttribute("size", size);
		m.addAttribute("count", count);
		m.addAttribute("max", max);
		return "list";
	}
	@RequestMapping("/userexit")
	public String userexit(HttpSession session) {
		session.invalidate();// ����session
		return "redirect:/index"; // ��ս��¼ҳ
	}
	// 登录方法
	@RequestMapping("/userlogin")
	public String userlogin(UserInfo user, HttpSession session) {
		System.out.println("controller userlogin  >>>>>>>>>>>>>>>>>>");
		UserInfo uinfo = userService.selectUserLogin(user);
		if (uinfo == null) {
			System.out.println("登录失败");
			session.setAttribute("uerr", "1");
			session.setAttribute("userinfo", user);
			return "redirect:/index";
		} else {
			System.out.println("登录成功");
			session.setAttribute("user", uinfo);
			return "redirect:/list";
		}
	}
	@RequestMapping("/userRegister")
	public String userRegister(UserInfo user, String code, Model model, HttpSession session) {
		if (this.checkCodeImg(code, session)) {// ��֤��У��ɹ�
			boolean boo = this.userService.userRegister(user);
			if (boo == true) {// ע��ɹ�
				session.setAttribute("regok", "1");
				// ���ص�¼ҳ
				return "redirect:/index";
			} else {
				session.setAttribute("reguser", user);
				return "redirect:/reg";
			}
		} else {
			session.setAttribute("codeerr", "1");
			return "redirect:/reg";
		}
	}
	//��ѯ�����û�
	@RequestMapping("/adminselectUsers")
	public String selectUsers(@RequestParam(defaultValue = "1") Integer index, @RequestParam(defaultValue = "5") Integer size ,String name , Model m) {
		PageInfo<UserInfo> users =  this.userService.selectAllUsers(index, size, name);
		m.addAttribute("pinfo", users);
		m.addAttribute("sname", name);
		return "/admin/userlist";
	}

	// У����֤��
	public boolean checkCodeImg(String code, HttpSession session) {
		// codeimg
		String cc = (String) session.getAttribute("codeimg");
		if (code.equalsIgnoreCase(cc)) {
			return true;
		}
		return false;
	}
}
